package net.web.inst.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.esj.basic.exception.MelonException;
import net.esj.basic.service.impl.DefaultServiceImpl;
import net.esj.basic.utils.DateUtils;
import net.esj.basic.utils.StringUtils;
import net.esj.basic.utils.Validators;
import net.melon.cata.inst.eg.region.Region;
import net.melon.order.inst.service.OrderService;
import net.melon.order.pojo.Order;
import net.melon.order.pojo.Order.OrderState;
import net.melon.product.inst.service.ProductService;
import net.melon.product.inst.service.impl.ClassifyCacheProxy;
import net.melon.product.pojo.Product;
import net.melon.product.pojo.ProductClassify;
import net.melon.product.pojo.ProductImage;
import net.melon.review.inst.service.ReviewService;
import net.melon.review.pojo.Review;
import net.melon.review.pojo.Review.ReviewState;
import net.melon.sso.pojo.Member;
import net.melon.stock.inst.service.StockService;
import net.melon.stock.pojo.Stock;
import net.melon.uploader.Uploader;
import net.web.dto.OrderDto;
import net.web.dto.ReviewDto;
import net.web.inst.service.NormalService;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

@Service
public class NormalServiceImpl extends DefaultServiceImpl implements NormalService {
	
	@Resource
	private OrderService orderService;
	
	@Resource
	private StockService stockService;
	
	@Resource
	private ClassifyCacheProxy classifyCacheProxy;
	
	@Resource
	private ProductService productService;
	
	@Resource
	private ReviewService reviewService;

	@Override
	public String saveOrder(String id, String colour, Long regionId, Member member) {
		Order order = new Order();
		order.setColour(colour);
		Stock stock = stockService.getStockByProductIdAndRegionId(id, regionId+"");
		if(stock==null || stock.getCount()==0){
			throw new MelonException("该地区库存不足"); 
		}
		order.setProduct(new Product(id));
		Region region = new Region();
		region.setId(regionId);
		order.setRegion(region);
		order.setMember(member);
		order.setPrice(stock.getProduct().getPrice());
		order.setPrepay(stock.getProduct().getReserveGold());
		orderService.saveOrder(order);
		return order.getId();
	}

	@Override
	public List<OrderDto> getMyOrder(Member member) throws Exception {
		List<Order> list = orderService.getOrderByMember(member);
		List<OrderDto> dtos = new ArrayList<OrderDto>();
		for(Order order : list){
			OrderDto dto = getOrdertoByOrder(order);
			dtos.add(dto);
		}
		return dtos;
	}
	

	@Override
	public OrderDto getOrderDetail(Member member, String id) {
		Order order = get(id, Order.class);
		if(order==null || order.getState()==OrderState.DELETE.getState()){
			throw new MelonException("订单不存在"); 
		}
		return getOrdertoByOrder(order);
	}

	private OrderDto getOrdertoByOrder(Order order) {
		OrderDto dto = new OrderDto();
		try {
			BeanUtils.copyProperties(dto, order);
			dto.setRegionName(order.getRegion().getName());
			dto.setParentName(order.getProduct().getName());
			ProductClassify pc = classifyCacheProxy.getClassify(order.getProduct().getClassifyId());
			if(pc!=null){
				dto.setClassifyName(pc.getName());
			}
			List<ProductImage> images = productService.getProductImageList(order.getProduct().getId());
			if(!Validators.isEmpty(images)){
				dto.setIcon(Uploader.getDownUrl(Product.ICON_SUB_DIR) + images.get(0).getIcon());
			}else{
				dto.setIcon(Uploader.getDownUrl(Product.ICON_SUB_DIR));
			}
		} catch (Exception e) {
		}
		return dto;
	}

	@Override
	public void deleteOrder(String id) throws MelonException, Exception {
		Order order = get(id, Order.class);
		if(order.getState()==OrderState.NO_PUT_CAR.getState()){
			throw new MelonException("未提车状态下不能删除");
		}
		order.setState(OrderState.DELETE.getState());
		update(order);
	}

	@Override
	public void review(ReviewDto reviewDto, Member member) {
		Review review = reviewService.getReviewByMember(member);
		boolean falg = false;
		if(review==null){
			review = new Review();
			review.setMemberId(member.getId());
			review.setGmtCreate(DateUtils.now());
			falg = true;
		}
		review = copyReview(review,reviewDto);
		review.setState(ReviewState.DRAFT.getState());
		if(falg){
			save(review);
		}else{
			update(review);
		}
	}

	private Review copyReview(Review review, ReviewDto reviewDto) {
		
		if(StringUtils.hasText(reviewDto.getIntentionCar())){
			review.setIntentionCar(reviewDto.getIntentionCar());
		}
		if(reviewDto.getIntentionPrice()!=0){
			review.setIntentionPrice(reviewDto.getIntentionPrice());
		}
		if(reviewDto.getProvince()!=0){
			review.setProvince(reviewDto.getProvince());
		}
		if(reviewDto.getCity()!=0){
			review.setCity(reviewDto.getCity());
		}
		if(reviewDto.getMarriage()!=0){
			review.setMarriage(reviewDto.getMarriage());
		}
		if(reviewDto.getHouse()!=0){
			review.setHouse(reviewDto.getHouse());
		}
		if(StringUtils.hasText(reviewDto.getName())){
			review.setName(reviewDto.getName());
		}
		if(StringUtils.hasText(reviewDto.getPhone())){
			review.setPhone(reviewDto.getPhone());
		}
		if(StringUtils.hasText(reviewDto.getAuthorization())){
			review.setAuthorization(reviewDto.getAuthorization());
		}
		String s = "";
		for(String identity : reviewDto.getIdentitys()){
			s += identity + ",";
		}
		if(StringUtils.hasText(s)){
			review.setIdentitys(s.substring(0, s.length() - 1));
		}
		
		s = "";
		for(String accountBook : reviewDto.getAccountBookList()){
			s += accountBook + ",";
		}
		if(StringUtils.hasText(s)){
			review.setAccountBook(s.substring(0, s.length() - 1));
		}
		s = "";
		for(String marriageCertificate : reviewDto.getMarriageCertificateList()){
			s += marriageCertificate + ",";
		}
		if(StringUtils.hasText(s)){
			review.setMarriageCertificate(s.substring(0, s.length() - 1));
		}
		s = "";
		for(String houseCertificate : reviewDto.getHouseCertificateList()){
			s += houseCertificate + ",";
		}
		if(StringUtils.hasText(s)){
			review.setHouseCertificate(s.substring(0, s.length() - 1));
		}
		
		s = "";
		for(String income : reviewDto.getIncomeList()){
			s += income + ",";
		}
		if(StringUtils.hasText(s)){
			review.setIncome(s.substring(0, s.length() - 1));
		}
		
		s = "";
		for(String bank : reviewDto.getBankList()){
			s += bank + ",";
		}
		if(StringUtils.hasText(s)){
			review.setBank(s.substring(0, s.length() - 1));
		}
		
		s = "";
		for(String driversLicense : reviewDto.getDriversLicenseList()){
			s += driversLicense + ",";
		}
		if(StringUtils.hasText(s)){
			review.setDriversLicense(s.substring(0, s.length() - 1));
		}
		
		return review;
	}
}
